#coding=utf-8
'''
Created on 2017年2月20日
@author: Lu.yipiao
'''
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#import tensorflow as tf
#import tensorflow.compat.v1 as tf
#tf.disable_v2_behavior()

#定义常量
rnn_unit=10       #hidden layer units
input_size=178
output_size=1
lr=0.0006     #学习率
#——————————————————导入数据——————————————————————
path='G:\大三下\人工智能与深度学习\预测组赛题\\'
#df=pd.read_csv(f)     #读入数据
#data=df.iloc[:,0:6].values  #取第3-10列

def read_data(path):
    train_data=pd.read_csv(path+'train.csv',dtype={'sale_date':object,'is_pro':object,},encoding='utf-8')
    train_data['goodsid']=train_data['goodsid'].astype(int)
    train_data['goodsid']=train_data['goodsid'].astype('object')

    test_data=pd.read_csv(path+'test.csv',dtype={'sale_date':object,'is_pro':object},encoding='utf-8')
    test_data['goodsid']=test_data['goodsid'].astype(int)
    test_data['goodsid']=test_data['goodsid'].astype('object')

    date_ch=pd.read_csv(path+'date_ch.csv',dtype={'dim_date_id':object,'year_code':object,'day_week_cn':object,'week_day_code':object,'is_weekend':object,'official_holiday_code':object},encoding='gbk')
    date_ch.rename(columns={'dim_date_id':'sale_date'}, inplace=True)

    goods_ch=pd.read_csv(path+'goods_ch.csv',dtype={'catg_l_id':object,'season_class':object},encoding='utf-8')
    goods_ch['goodsid']=goods_ch['goodsid'].astype(int)
    goods_ch['goodsid']=goods_ch['goodsid'].astype('object')
    del date_ch['official_holiday_name']
    del date_ch['festival_name']
    del goods_ch['season_class_name']
    del goods_ch['div_name']
    return train_data,test_data,date_ch,goods_ch

def merge_attr(train_data,test_data,date_ch,goods_ch):
    raw_train=pd.merge(train_data,date_ch,how='inner',on='sale_date')
    raw_train=pd.merge(raw_train,goods_ch,how='inner',on='goodsid')
    raw_test=pd.merge(test_data,date_ch,how='inner',on='sale_date')
    raw_test=pd.merge(raw_test,goods_ch,how='inner',on='goodsid')
    return raw_train,raw_test

def preprocessing(path):
    train_data,test_data,date_ch,goods_ch=read_data(path)
    raw_train,raw_test=merge_attr(train_data,test_data,date_ch,goods_ch)
    n_train=raw_train.shape[0]
    train_cols=list(range(3,6))+[7]+list(range(11,14))+[15]+[17]+[20]
    test_cols=list(range(2,5))+[6]+list(range(10,13))+[14]+[16]+[19]
    all_features=pd.concat((raw_train.iloc[:,train_cols], raw_test.iloc[:,test_cols]))
    labels=raw_train.iloc[:,[2]][:n_train]
    info=raw_test.iloc[:,[0,1]]

    numeric_features=all_features.dtypes[all_features.dtypes!='object'].index
    all_features[numeric_features]=all_features[numeric_features].apply(lambda x:(x-x.mean())/(x.std()))
    all_features[numeric_features]=all_features[numeric_features].fillna(0)
    
    attr_list=['year_code', 'day_week_cn','is_weekend','festival_code', 'season_class','catg_l_id','is_pro','week_day_code']
    all_features=pd.get_dummies(all_features,dummy_na=True,prefix=attr_list,columns=attr_list,dtype=int)
    
    train_features=all_features[:n_train]
    test_features=all_features[n_train:]
    train_features=pd.concat([train_features,labels],axis=1)
    
    return train_features,test_features,info
 #得到训练集和测试集数据
train_features,test_features,info=preprocessing(path)
train_data=train_features.iloc[:,0:].values
test_data=test_features.iloc[:,0:].values
#print(train_data)
print(test_data.shape)

#print(train_data[0])
#print(test_data[0])
data_train=test_data[0:100]
normalized_train_data=(data_train-np.mean(data_train,axis=0))/np.std(data_train,axis=0)
print(normalized_train_data)